package com.lancoo.znbkxx.teacher.diagnosis

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.View
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseActivity
import com.lancoo.znbkxx.teacher.model.data.Time
import kotlinx.android.synthetic.main.activity_achievement.*
import kotlinx.android.synthetic.main.item_achievement_markview.view.*


/**
 * @author : Admin
 * @date   : 2020/12/17
 * @desc   :
 */
class AchievementActivity : BaseActivity() {
    private val barEntries = mutableListOf<BarEntry>()
    private val lineEntries = mutableListOf<Entry>()
    private val xLabels = mutableListOf<String>()
    private val timeList by lazy { intent.getParcelableArrayListExtra<Time>("timeList") }
    private val totalScore by lazy { intent.getFloatExtra("totalScore", 140f) }
    override fun setLayout(): Int {
        return R.layout.activity_achievement
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        if (timeList.isEmpty()) {
            group.visibility = View.GONE
            tv_empty.visibility = View.VISIBLE
        } else {
            group.visibility = View.VISIBLE
            tv_empty.visibility = View.GONE
            initChart(timeList)
        }
    }

    override fun isNeedToolBar(): Boolean {
        return false
    }

    private fun initChart(timeList: ArrayList<Time>) {
        barEntries.clear()
        lineEntries.clear()
        xLabels.clear()
        timeList.forEachIndexed { index, time ->
            val barEntry = BarEntry(index.toFloat(), time.PersonalScore.toFloat())
            barEntries.add(barEntry)
            lineEntries.add(barEntry)
            xLabels.add(time.DayTime)
        }
        chart.apply {
            val barDataSet = BarDataSet(barEntries, "")
            barDataSet.color = Color.parseColor("#22d7bb")
            val barData = BarData(barDataSet)
            barData.apply {
                setDrawValues(true)
                setValueTextColor(Color.parseColor("#22d7bb"))
                setValueTextSize(13f)
                barWidth = if (timeList.size <= 5) 0.28f * timeList.size / 5 else 0.28f
            }
            val lineDataSet = LineDataSet(lineEntries, "")
            lineDataSet.apply {
                color = Color.parseColor("#ffd440")
                lineWidth = 1f
                setDrawCircleHole(false)
                setDrawCircles(false)
                mode = LineDataSet.Mode.CUBIC_BEZIER
            }
            val lineData = LineData(lineDataSet)
            lineData.setDrawValues(false)
            val combinedData = CombinedData()
            combinedData.setData(barData)
            combinedData.setData(lineData)
            data = combinedData
            axisLeft.apply {
                spaceBottom = 0f
                axisMinimum = 0f
                axisMaximum = totalScore + 5f
                textSize = 10f
                textColor = Color.parseColor("#999999")
            }
            val markerView =
                MyMarkView(this@AchievementActivity, R.layout.item_achievement_markview)
            markerView.chartView = this
            marker = markerView
            axisRight.isEnabled = false
            //关闭图例
            legend.isEnabled = false
            //关闭描述信息的显示
            description.isEnabled = false
            setPinchZoom(false)
            setScaleEnabled(false)
            //设置x轴显示在下方
            xAxis.apply {
                axisMinimum = -0.5f
                axisMaximum = timeList.size.toFloat() - 0.5f
                position = XAxis.XAxisPosition.BOTTOM
                isGranularityEnabled = true
                granularity = 1f
                labelCount = xLabels.size
                //设置X轴文本旋转角度
                labelRotationAngle = 0f
                setDrawAxisLine(false)
                setDrawGridLines(false)
                //设置X轴文本大小
                textSize = 11f
                textColor = Color.parseColor("#999999")
                valueFormatter = IndexAxisValueFormatter(xLabels) //设置x轴标签格式化器
            }
            if (timeList.size > 5) {
                setVisibleXRangeMaximum(5f)
            }
            animateXY(1000, 1000)
        }
    }

    inner class MyMarkView(context: Context, layoutResource: Int) :
        MarkerView(context, layoutResource) {
        @SuppressLint("SetTextI18n")
        override fun refreshContent(e: Entry?, highlight: Highlight?) {
            this.tv_hint.text = "预估成绩: ${highlight?.y}"
            super.refreshContent(e, highlight)
        }
    }
}